const path_ = eval('require')('path');
const fs = eval('require')('fs');

const walkTree = function (dir, childPath, excludePath) {
    const children = [];
    excludePath = (excludePath && excludePath instanceof Array) ? excludePath : [];
    childPath = childPath || '';
    fs.readdirSync(dir).forEach((filename) => {
        const path = path_.join(dir, filename);// dir+"/"+filename
        // if (filename === 'node_modules') {
        //     children.push({
        //         filename,
        //         path,
        //         childPath: path_.join(childPath, filename), // childPath + '/' + filename + '/',
        //         children: [],
        //     });
        //     return;
        // }
        if (excludePath.includes(filename)) {
            return;
        }
        const stat = fs.statSync(path);
        if (stat && stat.isDirectory()) {
            children.push({
                childPath: path_.join(childPath, filename), // childPath + '/' + filename + '/',
                filename,
                path,
                relatedPath: childPath,
                size: 0,
                children: walkTree(path, path_.join(childPath, filename), excludePath), // childPath + '/' + filename)
            });
        } else {
            children.push({
                filename,
                childPath: `${path_.join(childPath, filename)}`,
                path,
                relatedPath: childPath,
                size: stat.size,
            });
        }
    });
    return children;
};

const walk = function (dir, childPath, excludePath) {
    const children = [];
    excludePath = (excludePath && excludePath instanceof Array) ? excludePath : [];
    childPath = childPath || '';
    fs.readdirSync(dir).forEach((filename) => {
        const path = path_.join(dir, filename);// dir+"/"+filename
        if (excludePath.includes(filename)) {
            return;
        }
        // if (filename === 'node_modules') {
        //     children.push({
        //         filename,
        //         path,
        //         childPath: path_.join(childPath, filename), // childPath + '/' + filename + '/',
        //         relatedPath: childPath,
        //         isDir: 'unknown',
        //     });
        //     return;
        // }
        const stat = fs.statSync(path);
        if (stat && stat.isDirectory()) {
            children.push({
                childPath: path_.join(childPath, filename), // childPath + '/' + filename + '/',
                filename,
                path,
                relatedPath: childPath,
                isDir: true,
                size: 0,
            });
            children.splice(children.length, 0, ...walk(path, path_.join(childPath, filename), excludePath));
        } else {
            children.push({
                filename,
                childPath: `${path_.join(childPath, filename)}`,
                path,
                relatedPath: childPath,
                isDir: false,
                size: stat.size,
            });
        }
    });
    return children;
};

module.exports = {
    walk,
    walkTree,
};
